package hexoUrlUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName UrlUtil
 * @description:
 * @author: 荣燊
 * @create: 2022-04-12 18:27
 **/
public class UrlUtil {
    /**
     * regex:我们的正则字符串
     * 参数2 就是一大段文本，这里用data表示
     */
    private static String filterSpecialStr(String data) {
        String regex = "rongshen2020.com";
        //sb存放正则匹配的结果
        StringBuffer sb = new StringBuffer();
        //编译正则字符串
        Pattern p = Pattern.compile(regex);
        //利用正则去匹配
        Matcher matcher = p.matcher(data);
//        int i = 1;
        //如果找到了我们正则里要的东西
        while (matcher.find()) {
            //保存到sb中，"\r\n"表示找到一个放一行，就是换行
            data.replaceAll("rongshen2020.com", "rongshen.top");
            sb.append(matcher.group() + "\r\n");
//            i++;
        }
        return sb.toString();
    }

    //替换
    public static String transformURLIntoLinks(String text){
        String urlValidationRegex = "(https?|ftp)://(www\\d?|[a-zA-Z0-9]+)?.[a-zA-Z0-9-]+(\\:|.)([a-zA-Z0-9.]+|(\\d+)?)([/?:].*)?";
        Pattern p = Pattern.compile(urlValidationRegex);
        Matcher m = p.matcher(text);
        StringBuffer sb = new StringBuffer();
        while(m.find()){
            String found = m.group(0);
            m.appendReplacement(sb, "<a href='"+found+"'>"+found+"</a>");
        }
        m.appendTail(sb);
        return sb.toString();
    }

    public static void main(String[] args) throws IOException {
        StringBuffer buffer = new StringBuffer();
        BufferedReader bf= new BufferedReader(
                new FileReader(
                        "C:\\Users\\Administrator\\Desktop\\testMarkdown\\springcloud-alibaba之sentinel流量控制、熔断降级、系统负载.md"));
        String s = null;
        while((s = bf.readLine()) != null){//使用readLine方法，一次读一行
            buffer.append(s);
        }
        String xml = buffer.toString();
        // 打印读取到的内容
        System.out.println("----------------源文档---------------");
//        System.out.println(xml);
        System.out.println("----------------开始匹配url---------------");
        String s1 = xml.replaceAll("rongshen2020.com", "rongshen.top");
        System.out.println(s1);
        // 匹配url
//        String filterSpecialStr = filterSpecialStr(xml);
//        System.out.println("匹配结果：" + filterSpecialStr);
    }
}
